from ninja import FilterSchema, Field, ModelSchema
from django.db.models import Q
from typing import Optional

from models.models import WaterCardTransferRecord


class WaterCardRecordTransferFilterSchema(FilterSchema):
    """用户订单筛选"""
    op_user: Optional[str] = Field(default=0, description="操作人",
                                   q=["op_user__nickname__contains", "op_user__openid__contains",
                                      "op_user__phone_number__contains"])
    receive_user: Optional[str] = Field(default=0, description="接收人",
                                        q=["receive_user__nickname__contains", "receive_user__openid__contains",
                                           "receive_user__phone_number__contains"])
    time: Optional[dict] = None

    def filter_time(self, value: dict):
        if not value:
            return Q()
        if value.get('start') and not value.get('end'):
            return Q(time__gt=value.get('start'))
        if not value.get('start') and value.get('end'):
            return Q(time__lt=value.get('end'))
        if value.get('start') and value.get('end'):
            return Q(time__gt=value.get('start'), time__lt=value.get('end'))
        return Q()


class WaterCardTransferRecordResponseSchema(ModelSchema):
    class Config:
        model = WaterCardTransferRecord
        model_fields = "__all__"
